# TODO: Add comment
# 
# Author: yaping
# Time: 2013-6-3 
###############################################################################
#give an input file, point out which column it contains a p value, return another file whose last column is the FDR corrected P value, while the other part is the same from the input file.
#by default, the column contain the p value is the last column

colNum<-NULL
for (e in commandArgs(TRUE)) {
	ta = strsplit(e,"=",fixed=TRUE)
	if(! is.na(ta[[1]][2])) {
		
		if(ta[[1]][1] == "inputFn"){
			fn<-ta[[1]][2]
		}
		if(ta[[1]][1] == "colNum"){
			colNum<-as.numeric(ta[[1]][2])
		}
		if(ta[[1]][1] == "outputFn"){
			outputFn<-ta[[1]][2]
		}
		
	}
}

input<-read.table(fn,sep="\t",header=F)
if(is.null(colNum)){
	colNum<-length(input[1,])
}
input<-cbind(input,p.adjust(input[,colNum],method="BH"))

write.table(input,outputFn,  quote = F, sep = "\t",row.names = F, col.names = F)
